package judesart.app.service;

import java.net.URL;

import judesart.app.shared.UploadToken;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.FormUploadToken;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.util.AuthenticationException;

@Service("VideosService")
@Lazy(true)
public class VideosServiceImpl implements VideosService {
	
	private final static Log LOGGER = LogFactory.getLog(VideosServiceImpl.class);

    private final String developerKey = "AI39si5-taygVG1n2sHNi2_RDgHvrZE5QDgbJq8MR-WcmbqMtKYUDdMuAgHvBcFanW3e_dzNEtZTJrCx-mihgzvD6YhLJb6Hog";
    private final String applicationName = "JudithShaylor.com";
    private final String username = "pedrof.costa@gmail.com";
    private final String password = "pypLWkvp^";
    private YouTubeService service;

    public VideosServiceImpl() throws AuthenticationException {
    }

    public UploadToken getFormUploadToken(String title, String description,
            String location, String... tags) {
        VideoEntry newEntry = new VideoEntry();

        YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
        mg.setTitle(new MediaTitle());
        mg.getTitle().setPlainTextContent(title);
        mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "People"));
        mg.setKeywords(new MediaKeywords());
        mg.getKeywords().addKeyword("art");
        mg.getKeywords().addKeyword("exhibition");
        mg.setDescription(new MediaDescription());
        mg.getDescription().setPlainTextContent(
                "Video for art exhibition : " + title);
        mg.setPrivate(false);
        if (tags != null) {
            for (String tag : tags) {
                mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, tag));
            }
        }

        // newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
        newEntry.setLocation(location);

        try {
            URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken");
            FormUploadToken token = getService().getFormUploadToken(uploadUrl,
                    newEntry);
            System.out.println(token.getUrl());
            System.out.println(token.getToken());

            return new UploadToken(token.getUrl(), token.getToken());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    private YouTubeService getService(){
    	if (service == null){
	        try {
				service = new YouTubeService(applicationName, developerKey);
				service.setUserCredentials(username, password);
			} catch (AuthenticationException e) {
				LOGGER.error("Error authenticating youtube", e);
				throw new RuntimeException(e);
			}
    	}
    	return service;
    }
}
